home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / WindowMaker / src / Q.h < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-20  |  3.8 KB  |  161 lines

  1. /*
  2.  * Q.h -  support for ForAll, ThereExists, Count and Sum (i.e. quantifiers).
  3.  *
  4.  * Portability: this file uses the GNU C Statement Expression extension and
  5.  *    so requires GCC/C++ with extensions enabled (this is checked by the
  6.  *    header file).
  7.  * 
  8.  * Copyright (c) 1997 Phil Maker
  9.  * All rights reserved.
  10.  *
  11.  * Redistribution and use in source and binary forms, with or without
  12.  * modification, are permitted provided that the following conditions
  13.  * are met:
  14.  * 1. Redistributions of source code must retain the above copyright
  15.  *    notice, this list of conditions and the following disclaimer.
  16.  * 2. Redistributions in binary form must reproduce the above copyright
  17.  *    notice, this list of conditions and the following disclaimer in the
  18.  *    documentation and/or other materials provided with the distribution.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  21.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  24.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30.  * SUCH DAMAGE.
  31.  *
  32.  * Id: Q.h,v 1.1.1.1 1997/11/23 11:45:50 pjm Exp 
  33.  */
  34.  
  35.  
  36. #ifndef _Q_h_
  37. #define _Q_h_ 1
  38.  
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42.  
  43. #ifndef WITHOUT_NANA 
  44.  
  45. #ifndef __GNUC__ /* not compiling with GCC/G++ extensions */
  46. error 
  47. This file requires the GNU C/C++ "statement expression" extension;
  48. so use GCC/G++ with extensions enabled. If you have not got GCC then 
  49. misery follows though we may be able to do something about it
  50. in the future.
  51. #endif
  52.  
  53. /*
  54.  * A(i,c,n,a) - true iff a is true for all values generated by for(i;c;n) 
  55.  * 
  56.  * Note: local variables can be introduced in this statement even in C.
  57.  */
  58.  
  59. #define A(i,c,n,a) /* ForAll */ \
  60.     ({ \
  61.         int _A_result = 1; \
  62.         i; \
  63.         while(c) { \
  64.             if(!(a)) { \
  65.                 _A_result = 0; \
  66.                 break; \
  67.             } \
  68.             n; \
  69.         } \
  70.         _A_result; \
  71.     })
  72.  
  73. /* 
  74.  * E(i,c,n,a) - true iff exists any true a for values generated by for(i;c;n)
  75.  */
  76.  
  77. #define E(i,c,n,a) /* Exists */ \
  78.     ({ \
  79.         int _E_result = 0; \
  80.         i; \
  81.         while(c) { \
  82.             if(a) { \
  83.                 _E_result = 1; \
  84.                 break; \
  85.             } \
  86.             n; \
  87.         } \
  88.         _E_result; \
  89.     })
  90.  
  91. /* 
  92.  * C(i,c,n,a) - count the number of times a is true over the values
  93.  * generated by for(i;c;n)
  94.  */
  95.  
  96. #define C(i,c,n,a) /* Count */ \
  97.     ({ \
  98.         long _C_result = 0; \
  99.         i; \
  100.         while(c) { \
  101.             if(a) { \
  102.                 _C_result++; \
  103.             } \
  104.             n; \
  105.         } \
  106.         _C_result; \
  107.     })
  108.  
  109. /*
  110.  * E1(i,c,n,a) - exists a single value generated by for(i;c;n)
  111.  * suchthat i is true.
  112.  */
  113.  
  114. #define E1(i,c,n,a) (C(i,c,n,a) == 1) /* There Exists 1 */
  115.  
  116. /*
  117.  * S(i,c,n,v) - sum of v over the values generated by for(i;c;n)
  118.  */
  119.  
  120. #define S(i,c,n,v) \
  121.     ({ \
  122.         i; \
  123.         typeof(v) _S_result = 0; \
  124.         while(c) { \
  125.             _S_result += (v); \
  126.             n; \
  127.         } \
  128.         _S_result; \
  129.     })
  130.  
  131. /*
  132.  * P(i,c,n,v) - product of v over the values generated by for(i;c;n)
  133.  */
  134.  
  135. #define P(i,c,n,v) \
  136.     ({ \
  137.         i; \
  138.         typeof(v) _P_result = 1; \
  139.         while(c) { \
  140.             _P_result *= (v); \
  141.             n; \
  142.         } \
  143.         _P_result; \
  144.     })
  145.  
  146. #else /* defined(WITHOUT_NANA) */
  147.  
  148. /*
  149.  * we don't produce any empty stubs for Q.h when compiling without nana
  150.  * since calls to A(...), etc should only occur in stubbed out code such
  151.  * as I(...).
  152.  */
  153.  
  154. #endif /* !defined(WITHOUT_NANA) */
  155. #ifdef __cplusplus
  156. }
  157. #endif
  158.  
  159. #endif /* _Q_h_ */
  160.  
  161.